package com.dashan.servlets;

import static com.dashan.Constants.SESSIONID;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easymock.EasyMock;

import com.dashan.backend.datastore.LocalDatastoreTestCase;

public class HttpServletTestCase extends LocalDatastoreTestCase {
    
    protected HttpServletRequest mockReq;
    protected HttpServletResponse mockResp;
    private ByteArrayOutputStream output;
    private PrintWriter writer;
    @Override
    public void setUp() throws Exception {
        super.setUp();
        mockReq = createMock(HttpServletRequest.class);
        mockResp = createMock(HttpServletResponse.class);
        
        expect(mockReq.getParameter(SESSIONID)).andReturn("0").anyTimes();
        
        // Redirect response to a String buffer.
        output = new ByteArrayOutputStream(1000);
        writer = new PrintWriter(output);
        expect(mockResp.getWriter()).andReturn(writer).anyTimes();
        replay(mockResp);
    }
    
    @Override
    public void tearDown() throws Exception {
        EasyMock.verify(mockReq);
        EasyMock.verify(mockResp);
        super.tearDown();
    }
    
    public String getResponse() {
        writer.close();
        return output.toString();
    }
}
